(function() {
	var $ = require(L('$')), _ = require(L('_'));
	var enumObj = require(L('enumeration'));
	var pagingSize = parseInt($.myDesiresPagingSize());
	if ($.is_debug()) {
		pagingSize = parseInt(L('debug_paging_size'));
	}

	var win = $.createWindow(L('win_mydesires'), true);
	win.data = {};
	var ui = $.layout('mydesires/view');
	ui.top.add($.ui('partial').scan_and_take_photo());
	win.nav.window.add([$.ui('navbar')(false, true), ui.view]);

	var sortByObj = $.ui('ddl').sortBy(function(e) {
		ui.table.currentSortValue = e;
		ui.table.filter();
	});
	ui.filter.add(sortByObj);
	ui.filter.sortByObj = sortByObj;
	var refineObj = $.ui('ddl').refine(function(e) {
		ui.table.currentRefineValue = e;
		ui.table.filter();
	});
	ui.filter.add(refineObj);
	ui.filter.refineObj = refineObj;

	ui.table.filter = function() {
		$.mask();
		var filteredData = FilterData(ui.table.currentSortValue, ui.table.currentRefineValue, win.MergedItems);
		win.filterDataResult = filteredData;
		ui.table.render_wrapper(filteredData);
		ui.table.render_rows(filteredData);
		$.mask(false);
	};
	ui.table.render = function(data) {
		ui.table.render_wrapper(data);
		ui.table.render_rows(data);
	};
	ui.table.render_wrapper = function(data) {
		ui.table.isBuilt = true;
		ui.table.scrollToTop(0);
		ui.title.text = data.length > 1 ? data.length + ' items found' : data.length + ' item found';
	};
	ui.table.render_rows = function(data) {
		var tableData = new Array();
		ui.table.setData(tableData);
		if (data.length == 0 && win.data.UGDs.length == 0) {
			var noitemUI = $.layout('mydesires/no_item');
			noitemUI.content.html = $.getStaticContent().MyDesireNoItem;
			$.on('load', noitemUI.content, function() {
				ui.table.scrollToTop(0);
			});
			tableData.push(noitemUI.row);
			ui.table.editable = false;
			$.mask(false);
			ui.table.data = tableData;
		} else {
			var startIndex = 0;
			var endIndex = startIndex + pagingSize;
			//tableData = BuildRowPaging(tableData, data, startIndex, endIndex);
			LoadMoreRows(data);
			//tableData.push($.ui('partial').empty_row());
			ui.table.editable = true;
		}
		// ui.table.data = tableData;
		ui.table.scrollToTop(0);
	};
	ui.table.reset = function() {
		ui.table.data = new Array();
	};
	$.click(ui.table, function(e) {
		if (e && e.row && e.row.ugd) {
			var ugd = e.row.ugd;
			if (e.source.sharing) {
				if (!ugd.IsSubmitted) {
					$.a(L('ugd_submit_to_share'), 'Share', null, function() {
						$.win('ugd/form').update(e.row.ugd);
					});
					return;
				}
				$.ui('popup').share({
					fb : {
						link : ugd.ShareFbLink,
						name : ugd.ShareFbName,
						message : '',
						caption : ugd.ShareFbCaption,
						description : ugd.ShareFbDescription,
						picture : ugd.ShareFbPicture,
					},
					twitter : {
						message : ugd.ShareTwitterMessage
					},
					pinterest : {
						link : ugd.ShareFbLink,
						picture : ugd.ShareFbPicture,
						description : ugd.SharePinterestDescription
					},
					email : {
						desireID : ugd.DesireID
					}
				}, function(type) {
					// e.row.active_share_icon(type);
					if (e && e.row && e.row.share_icons && e.row.share_icons.active)
						e.row.share_icons.active(type);
					else
						$.w('#CRITICAL ERROR : I dont know why e.row.active_share_icon not work after paging!');
					if (type == $.enum('share').Pinterest)
						ugd.PinterestShareTimes = 1;
					if (type == $.enum('share').Facebook)
						ugd.FacebookShareTimes = 1;
					if (type == $.enum('share').Twitter)
						ugd.TwitterShareTimes = 1;
					if (type == $.enum('share').Email)
						ugd.EmailShareTimes = 1;
					e.row.ugd = ugd;
					if (type == $.enum('share').Email)
						return;
					$.webservice().updateShareUGD(ugd.DesireID, type, function(response) {
						var result = JSON.parse(response);
						if (result.Success)
							$.a(L('ugd_share_success'), L('ugd_success_title'));
					});
				}).open();
			} else {
				if (e.row.ugd.IsSubmitted) {
					$.win('ugd/details').display(e.row.ugd);
					$.win('my_desires').nav.push($.win('ugd/details'));
				} else {
					$.win('ugd/form').update(e.row.ugd);
				}
			}
		} else if (e && e.source && e.row && e.row.product) {
			var id = e.source.id, product = e.row.product;
			if (id == 'icon_add_to_cart') {
				var saleCampaignID = product.OfferName ? product.SaleCampaignOfferCustomerDefinedBundleID : '';
				$.addToShoppingBag(product.ProductVariantID, product.RetailerStoreID, saleCampaignID);
			} else if (e.source.sharing) {
				$.ui('popup').share({
					fb : {
						link : product.ShareFbLink,
						name : product.ShareFbName,
						message : product.ShareFbCaption,
						caption : '',
						description : product.ShareFbDescription,
						picture : product.ShareFbPicture,
					},
					twitter : {
						message : product.ShareTwitterMessage
					},
					pinterest : {
						link : product.ShareFbLink,
						picture : product.ShareFbPicture,
						description : product.SharePinterestDescription
					},
					email : {
						productVariantID : product.ProductVariantID,
						retailerStoreID : product.RetailerStoreID
					}
				}, function(type) {
					if (e && e.row && e.row.active_share_icon)
						e.row.active_share_icon(type);

					if (type == $.enum('share').Email)
						return;
					$.webservice().updateShareInfo(product.ProductVariantID, product.RetailerStoreID, type, '', function(response) {
						var result = JSON.parse(response);
						if (result.Success)
							$.a(L('message_share_success'), L('message_share_success_title'));
					});
				}).open();
			} else {
				$.win('product_details').display(product.ProductVariantID, product.RetailerStoreID);
				$.win('my_desires').nav.push($.win('product_details'));
			}
		}
	});
	$.on('delete', ui.table, function(e) {
		ui.table.setData(ui.table.data);
		//$.mask();
		//ui.table.isBuilt = false;
		if (e && e.row && e.row.ugd) {
			Ti.API.info('reached here 1');
			win.filterDataResult = DeleteUGDItemInMergedItems(e.row.ugd.DesireID, win.filterDataResult);
			win.MergedItems = DeleteUGDItemInMergedItems(e.row.ugd.DesireID, win.MergedItems);
			$.webservice().deleteUGD(e.row.ugd.DesireID, function(e) {
				var result = JSON.parse(e);
				if (result)
					ui.title.text = result.ItemCount;
			});

		} else if (e && e.row && e.row.product) {
			Ti.API.info('reached here 2');
			win.filterDataResult = DeleteItemInMergedItems(e.row.product.WishListID, win.filterDataResult);
			win.MergedItems = DeleteItemInMergedItems(e.row.product.WishListID, win.MergedItems);
			$.webservice().deleteWishlistList(e.row.product.WishListID, function(e) {
				var result = JSON.parse(e);
				if (result)
					ui.title.text = result.ItemCount;
			});
		}
		//$.mask(false);
	});

	win.display = function(callBack) {
		$.opacity_show(ui.view, false);
		$.mask();
		var applyDefaultFilter = false;
		if (ui.table.currentSortValue !== enumObj.SortTypeMyDesire.SortBy.value || ui.table.currentRefineValue !== enumObj.RefineTypePurchase.Refine.value)
			applyDefaultFilter = true;
		ui.table.currentSortValue = enumObj.SortTypeMyDesire.SortBy.value;
		ui.filter.sortByObj.updateSelectedValue(enumObj.SortTypeMyDesire.SortBy);
		ui.table.currentRefineValue = enumObj.RefineTypePurchase.Refine.value;
		ui.filter.refineObj.updateSelectedValue(enumObj.RefineTypePurchase.Refine);
		$.ui('bottom').active(0);
		$.webservice().getMyDesires(function(responseText, nothingNew) {
			if (nothingNew && ui.table.isBuilt) {
				if (applyDefaultFilter) {
					ui.table.reset();
					var mergedItems = MergeDesireItemsToProductList(win.data.UGDs, win.data.ProductItem);
					mergedItems = FilterData(ui.table.currentSortValue, ui.table.currentRefineValue, mergedItems);
					win.MergedItems = mergedItems;
					ui.table.render(win.MergedItems);
				}
				$.opacity_show(ui.view);
				$.mask(false);
				return;
			}
			if (ui.table.isBuilt)
				ui.table.reset();
			win.data = JSON.parse(responseText);
			Ti.API.info('win display item - UGDs: ' + (win.data.UGDs ? win.data.UGDs.length : 0) + ' - ProductItemCount: ' + (win.data.ProductItem ? win.data.ProductItem.length : 0));
			var mergedItems = MergeDesireItemsToProductList(win.data.UGDs, win.data.ProductItem);
			mergedItems = FilterData(ui.table.currentSortValue, ui.table.currentRefineValue, mergedItems);
			win.MergedItems = mergedItems;
			win.filterDataResult = mergedItems;
			ui.title.text = win.data.ItemCount;
			$.opacity_show(ui.view);
			ui.table.render(win.MergedItems);
			//win.data ? GetListOfMydesireRetailer(win.data.ProductItem) : GetListOfMydesireRetailer(null);
			//$.mask(false);
		});
	};
	win.hideView = function() {
		$.opacity_show(ui.view, false);
	};
	win.reset = function() {
		ui.table.isBuilt = false;
		ui.table.reset();
	};

	$.scrollEnd(ui.table, function(e) {
		var height = e.contentSize.height - e.size.height;
		if (e.contentOffset.y + 243 > height) {
			/*var tableData = ui.table.data;
			 var startIndex = tableData[0] ? tableData[0].rowCount : 0;
			 var endIndex = startIndex + pagingSize;
			 $.i('[scroll_end] startIndex: ' + startIndex + ' - endIndex: ' + endIndex + ' - filterDataResult: ' + win.filterDataResult.length);
			 if (startIndex <= win.filterDataResult.length) {
			 tableData = BuildRowPaging(tableData, win.filterDataResult, startIndex, endIndex);
			 ui.table.data = tableData;
			 }*/
			var total = ui.table.data && ui.table.data[0] && ui.table.data[0].rows ? ui.table.data[0].rows.length : 0;
			if (total < win.MergedItems.length + 2) {
				LoadMoreRows(win.filterDataResult);
			}
		}
	});

	function BuildRowPaging(tableData, productItem, startIndex, endIndex) {
		var itemCount = productItem.length;
		if (endIndex < 0)
			endIndex = itemCount;

		for (var i = startIndex; i < endIndex; i++) {
			if (i < itemCount) {
				var item = productItem[i];
				if (item.isUGD) {
					tableData.push(UGDView(item, i));
				} else {
					tableData.push(OneProductView(item, i));
				}
			} else {
				break;
			}
		}
		return tableData;
	}

	function LoadMoreRows(data) {
		if (data.length == 0)
			return;
		var pagingSize = $.myDesiresPagingSize() ? parseInt($.myDesiresPagingSize()) : 5;
		var lastItemIndex = ui.table.data && ui.table.data[0] && ui.table.data[0].rows ? ui.table.data[0].rows.length : 0;
		var max = data.length;
		if (lastItemIndex > 0) {
			if (lastItemIndex == max + 1) {
				ui.table.updateRow(lastItemIndex - 1, {
					height : 1,
					visisble : false
				});
				return;
			}
			ui.table.deleteRow(lastItemIndex - 1, {
				animationStyle : Titanium.UI.iPhone.RowAnimationStyle.NONE
			});
			lastItemIndex--;
		}
		for (var i = lastItemIndex; i < lastItemIndex + pagingSize && i < data.length; i++) {
			var item = data[i];
			var view = data[i].isUGD ? UGDView(data[i], i) : OneProductView(data[i], i);
			ui.table.appendRow(view, {
				animationStyle : Titanium.UI.iPhone.RowAnimationStyle.NONE
			});
		}
		var loadingrow = $.layout('mydesires/partial/loading_row').row;
		ui.table.appendRow(loadingrow, {
			animationStyle : Titanium.UI.iPhone.RowAnimationStyle.NONE
		});
	};

	function MergeDesireItemsToProductList(desireList, productList) {
		var result = new Array();
		if (desireList) {
			for (var i = 0; i < desireList.length; i++) {
				desireList[i].isUGD = true;
				result.push(desireList[i]);
			}
		}
		if (productList) {
			for (var i = 0; i < productList.length; i++) {
				var item = productList[i];
				var productItem = {
					'isUGD' : false,
					'WishListID' : item.WishListID,
					'ProductVariantID' : item.ProductVariantID,
					'ProductVariantName' : item.ProductVariantName,
					'ImageLink' : item.ImageLink,

					'Price' : item.Price,
					'OfferName' : item.OfferName,
					'OfferNormalPrice' : item.OfferNormalPrice,
					'SaleOffPrice' : item.SaleOffPrice,
					'SaleCampaignOfferCustomerDefinedBundleID' : item.SaleCampaignOfferCustomerDefinedBundleID,
					'BundledProductImageLink' : item.BundledProductImageLink,
					'BundledProductName' : item.BundledProductName,
					'FormattedMobilePrice' : item.FormattedMobilePrice,
					'FormattedMobileSaleOffPrice' : item.FormattedMobileSaleOffPrice,
					'FormattedOfferNormalPrice' : item.FormattedOfferNormalPrice,
					'FormattedPrice' : item.FormattedPrice,
					'HighestPriceIndex' : item.HighestPriceIndex,
					'LiveOfferIndex' : item.LiveOfferIndex,
					'LowestPriceIndex' : item.LowestPriceIndex,
					'NewestIndex' : item.NewestIndex,
					'OldestIndex' : item.OldestIndex,

					'RetailerID' : item.RetailerID,
					'RetailerFullName' : item.RetailerFullName,
					'RetailerStoreID' : item.RetailerStoreID,
					'RetailerStoreName' : item.RetailerStoreName,

					'EmailShareTimes' : item.EmailShareTimes,
					'FacebookShareTimes' : item.FacebookShareTimes,
					'SmsShareTimes' : item.SmsShareTimes,
					'TwitterShareTimes' : item.TwitterShareTimes,
					'PinterestShareTimes' : item.PinterestShareTimes,

					'ShareFbCaption' : item.ShareFbCaption,
					'ShareFbDescription' : item.ShareFbDescription,
					'ShareFbLink' : item.ShareFbLink,
					'ShareFbName' : item.ShareFbName,
					'ShareFbPicture' : item.ShareFbPicture,
					'SharePinterestDescription' : item.SharePinterestDescription,
					'ShareTwitterMessage' : item.ShareTwitterMessage

					//'BundledRetailerStore' : "",
					//'FormattedBundledPrice' : "",
					//'FormattedMobileRRP' : "",
					//'FormattedMobileRemainingDate' : "",
					//'FormattedPurchasedSeperatelyPrice' : "",
					//'FormattedRRP' : "",
					//'FormattedSaleOffPrice' : "",
					//'GooglePlusShareTimes' : 0,
					//'OfferDescription' : null,
					//'OfferIsExpired' : false,
					//'OfferType' : 0,
					//'ProductDetailLink' : "",
					//'RRP' : 0,
					//'IsWishList' : null,
				};
				result.push(productItem);
			};
		}
		return result;
	}

	function GetListOfMydesireRetailer(productList) {
		if (productList) {
			var MyDesireRetailerItems = new Array();

			var retailerIDs = new Array();
			var productVariantIDs = new Array();

			for (var i = 0; i < productList.length; i++) {
				var currentRetailerID = productList[i].RetailerID;
				var currentProductVariantID = productList[i].ProductVariantID;

				if (currentRetailerID != null && currentRetailerID != undefined) {
					var retailerIDIndex = -1;
					for (var j = 0; j < retailerIDs.length; j++) {
						if (currentRetailerID === retailerIDs[j]) {
							retailerIDIndex = j;
							break;
						}
					}
					if (retailerIDIndex < 0) {
						retailerIDs.push(currentRetailerID);
						retailerIDIndex = retailerIDs.length - 1;
					}

					if (currentProductVariantID != null && currentProductVariantID != undefined) {
						var listProductVariantID = productVariantIDs[retailerIDIndex];
						if (listProductVariantID) {
							listProductVariantID.push(currentProductVariantID);
							productVariantIDs[retailerIDIndex] = listProductVariantID;
						} else {
							listProductVariantID = new Array();
							listProductVariantID.push(currentProductVariantID);
							productVariantIDs.push(listProductVariantID);
						}
					} else {
						var listProductVariantID = new Array();
						listProductVariantID.push(currentProductVariantID);
						productVariantIDs.push(listProductVariantID);
					}
				}
			};

			for (var i = 0; i < retailerIDs.length; i++) {
				MyDesireRetailerItems.push($.model('mydesire_retailer')(retailerIDs[i], productVariantIDs[i]));
			}

			//Ti.API.info('mydesire create store list:'  +  JSON.stringify(MyDesireRetailerItems));

			$.mydesireRetailerItems(JSON.stringify(MyDesireRetailerItems));
		} else {
			$.mydesireRetailerItems(null);
		}
	};

	function DeleteItemInMergedItems(id, source) {
		var result = new Array();
		if (source) {
			var mergedItems = source;
			if (mergedItems) {
				for (var i = 0; i < mergedItems.length; i++) {
					if (mergedItems[i].isUGD === false) {
						if (mergedItems[i].WishListID !== id)
							result.push(mergedItems[i]);
					} else
						result.push(mergedItems[i]);
				}
			}
		}
		return result;
	}

	function DeleteUGDItemInMergedItems(id, source) {
		var result = new Array();
		if (source) {
			var mergedItems = source;
			if (mergedItems) {
				for (var i = 0; i < mergedItems.length; i++) {
					if (mergedItems[i].isUGD === true) {
						if (mergedItems[i].DesireID !== id)
							result.push(mergedItems[i]);
					} else
						result.push(mergedItems[i]);
				}
			}
		}
		return result;
	}

	function OneProductView(product, index) {
		var share_icons = partial.share_icons(product);
		if (product.OfferName) {
			if (product.BundledProductName) {
				var ui = $.layout('mydesires/bundle_offer');
				ui.offerName.text = product.OfferName;
				ui.bundledProductName.text = product.BundledProductName;
				ui.bundledRetailerName.text = product.RetailerFullName;
				ui.image1.add(partial.image(product.ImageLink));
				ui.image2.add(partial.image(product.BundledProductImageLink));
				ui.right.add(partial.price(product));
				//ui.right.add(partial.share_icons(product));
				ui.right.add(share_icons);
			} else {
				var ui = $.layout('mydesires/single_offer');
				ui.offerName.text = product.OfferName;
				ui.image.add(partial.image(product.ImageLink));
				ui.right.add(partial.price(product));
				//ui.right.add(partial.share_icons(product));
				ui.right.add(share_icons);
			}
		} else {
			var ui = $.layout('mydesires/normal');
			ui.price.text += product.FormattedMobilePrice;
			ui.image.add(partial.image(product.ImageLink));
			//ui.right.add(partial.share_icons(product));
			ui.right.add(share_icons);
		}
		ui.row.selectionStyle = Ti.UI.iPhone.TableViewCellSelectionStyle.NONE;
		ui.row.backgroundColor = index % 2 == 1 ? '#f7f7f7' : '#fff';
		ui.icon_cart.image = index % 2 == 1 ? L('icon_cart_gray') : L('icon_cart');
		ui.productName.text = product.ProductVariantName;
		ui.retailerName.text = product.RetailerStoreName;
		ui.row.product = product;
		ui.row.active_share_icon = function(type) {
			share_icons.active(type);
		}
		return ui.row;
	};

	function UGDView(ugd, index) {
		var ui = $.layout('mydesires/ugd');
		ui.price.text += ugd.FormattedPrice ? ugd.FormattedPrice : '-';
		ui.price.visible = $.isNull(ugd.Price) ? false : true;
		ui.image.add($.ui('partial').mydesire_ugd_image(ugd.ImagePath, function(e) {
			$.repeat(function() {
				ui.image.updateLayout({
					height : e
				});
				ui.row.updateLayout({
					minHeight : e + 15,
					height : Ti.UI.SIZE
				});
			}, 20);
		}));
		var share_icons = partial.share_icons(ugd);
		ui.right.add(share_icons);
		ui.row.selectionStyle = Ti.UI.iPhone.TableViewCellSelectionStyle.NONE;
		ui.row.backgroundColor = index % 2 == 1 ? '#f7f7f7' : '#fff';
		if (ugd.IsSubmitted)
			ui.icon_cart.image = index % 2 == 1 ? L('icon_heart_2_gray') : L('icon_heart_2');
		else
			ui.icon_cart.image = index % 2 == 1 ? L('icon_heart_1_gray') : L('icon_heart_1');
		ui.productName.text = ugd.RetailerName;
		if ($.is_debug())
			ui.productName.text = '#' + (index + 1) + ' : ' + ugd.RetailerName;
		ui.retailerName.text = ugd.RetailerStoreName;
		ui.row.ugd = ugd;
		ui.row.share_icons = share_icons;
		ui.row.active_share_icon = function(type) {
			share_icons.active(type);
		}
		return ui.row;
	};

	function FilterData(currentSortBy, currentRefine, productItems) {
		var filteredProductItems = new Array();
		if (productItems) {
			var productItemLength = productItems.length;
			if (currentSortBy === enumObj.SortTypeMyDesire.SortBy.value || currentSortBy === enumObj.SortTypeMyDesire.Newest.value) {
				if (currentRefine === enumObj.RefineTypeMyDesire.LiveOffers.value) {
					$.i(' SortBy OR Newest - LiveOffers');
					for (var i = 0; i < productItemLength; i++) {
						if (productItems[i].NewestIndex > 0 && productItems[i].LiveOfferIndex > 0) {
							filteredProductItems.push(productItems[i]);
						}
					}
					filteredProductItems = _.sortBy(filteredProductItems, function(item) {
						return item.NewestIndex;
					});
				} else {
					$.i(' SortBy OR Newest - Refine');
					for (var i = 0; i < productItemLength; i++) {
						if (productItems[i].NewestIndex > 0) {
							filteredProductItems.push(productItems[i]);
						}
					}
					filteredProductItems = _.sortBy(filteredProductItems, function(item) {
						return item.NewestIndex;
					});
				}
			} else if (currentSortBy === enumObj.SortTypeMyDesire.Oldest.value) {
				if (currentRefine === enumObj.RefineTypeMyDesire.LiveOffers.value) {
					$.i(' Oldest - LiveOffers');
					for (var i = 0; i < productItemLength; i++) {
						if (productItems[i].OldestIndex > 0 && productItems[i].LiveOfferIndex > 0) {
							filteredProductItems.push(productItems[i]);
						}
					}
					filteredProductItems = _.sortBy(filteredProductItems, function(item) {
						return item.OldestIndex;
					});
				} else {
					$.i(' Oldest - Refine');
					for (var i = 0; i < productItemLength; i++) {
						if (productItems[i].OldestIndex > 0) {
							filteredProductItems.push(productItems[i]);
						}
					}
					filteredProductItems = _.sortBy(filteredProductItems, function(item) {
						return item.OldestIndex;
					});
				}
			} else if (currentSortBy === enumObj.SortTypeMyDesire.PriceHighest.value) {
				if (currentRefine === enumObj.RefineTypeMyDesire.LiveOffers.value) {
					$.i(' Price Highest - LiveOffers');
					for (var i = 0; i < productItemLength; i++) {
						if (productItems[i].HighestPriceIndex > 0 && productItems[i].LiveOfferIndex > 0) {
							filteredProductItems.push(productItems[i]);
						}
					}
					filteredProductItems = _.sortBy(filteredProductItems, function(item) {
						return item.HighestPriceIndex;
					});
				} else {
					$.i(' Price Highest - Refine');
					for (var i = 0; i < productItemLength; i++) {
						if (productItems[i].HighestPriceIndex > 0) {
							filteredProductItems.push(productItems[i]);
						}
					}
					filteredProductItems = _.sortBy(filteredProductItems, function(item) {
						return item.HighestPriceIndex;
					});
				}
			} else if (currentSortBy === enumObj.SortTypeMyDesire.PriceLowest.value) {
				if (currentRefine === enumObj.RefineTypeMyDesire.LiveOffers.value) {
					$.i(' Price Lowest - LiveOffers');
					for (var i = 0; i < productItemLength; i++) {
						if (productItems[i].LowestPriceIndex > 0 && productItems[i].LiveOfferIndex > 0) {
							filteredProductItems.push(productItems[i]);
						}
					}
					filteredProductItems = _.sortBy(filteredProductItems, function(item) {
						return item.LowestPriceIndex;
					});
				} else {
					$.i(' Price Lowest - Refine');
					for (var i = 0; i < productItemLength; i++) {
						if (productItems[i].LowestPriceIndex > 0) {
							filteredProductItems.push(productItems[i]);
						}
					}
					filteredProductItems = _.sortBy(filteredProductItems, function(item) {
						return item.LowestPriceIndex;
					});
				}
			}

		}
		return filteredProductItems;
	};

	var partial = {};
	partial.price = function(product) {
		var view = $.createWebView('data/html/mydesires_bundled_price.html', {
			width : 155,
			height : product.BundledProductName ? 80 : 35,
			right : 0,
			top : 5,
			backgroundColor : 'transparent'
		});
		view.onload(function() {
			if (product.BundledProductName) {
				view.val('val_1', product.FormattedOfferNormalPrice);
				view.val('val_3', product.FormattedMobileSaleOffPrice);
				view.val('val_2', '-$' + (product.OfferNormalPrice - product.SaleOffPrice).toFixed(2));
				view.css('line1', 'display', 'block');
				view.css('line2', 'display', 'block');
				view.css('name3', 'display', 'block');
			} else {
				view.val('val_1', product.FormattedOfferNormalPrice);
				view.val('val_2', product.FormattedMobileSaleOffPrice);
			}
		});
		view.disableScroll();
		return view;
	};
	partial.image = function(image) {
		return $.ui('partial').mydesires_product_image(image);
	};
	partial.share_icons = function(product) {
		var pui = $.layout('mydesires/partial/share_icons');
		if (product.PinterestShareTimes > 0)
			pui.iconPinterest.image = L('icon_pinterest_on');
		if (product.FacebookShareTimes > 0)
			pui.iconFacebook.image = L('icon_facebook_on');
		if (product.TwitterShareTimes > 0)
			pui.iconTwitter.image = L('icon_twitter_on');
		if (product.EmailShareTimes > 0)
			pui.iconEmail.image = L('icon_email_on');
		pui.view.active = function(type) {
			if (type == $.enum('share').Pinterest)
				pui.iconPinterest.image = L('icon_pinterest_on');
			if (type == $.enum('share').Facebook)
				pui.iconFacebook.image = L('icon_facebook_on');
			if (type == $.enum('share').Twitter)
				pui.iconTwitter.image = L('icon_twitter_on');
			if (type == $.enum('share').Email)
				pui.iconEmail.image = L('icon_email_on');
		};
		return pui.view;
	};

	module.exports = win;
})();
